package com.jackrain.nea.shiro.stateful;

import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.WebUtils;
import com.jackrain.nea.web.LoadUserenvCmd;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.security.Security4Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * Created by Lucky dog on 2016/7/19.
 */
@Slf4j
@Component
public  class UserRealm extends AuthorizingRealm {


    //获取身份验证相关信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        /* 这里编写登陆认证代码 */
        CustomizedToken token = (CustomizedToken)authcToken;
        User user = null;
        Subject subject = SecurityUtils.getSubject();
        Locale locale=LocaleContextHolder.getLocale();
        try {
            user = Security4Utils.getUser(token.getUsername());
            LoadUserenvCmd loadUserenvCmd = (LoadUserenvCmd) ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.web.LoadUserenvCmd", "cp", "1.0");
            user.setUserenv(loadUserenvCmd.getUserenv(user));
//            user.setUserenv(QueryUtils.getUserenv(user));
            ((UserImpl)user).setLocale(locale);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }

        if(token.getLoginType() == LoginType.NO_PASSWORD){
            token.setPassword(user.getPasswordHash().toCharArray());
        } else if(token.getLoginType() == LoginType.WITH_PASSWORD){
            String tokPassword = String.valueOf(token.getPassword());
            String password = WebUtils.encryptBasedDes(tokPassword, user.getPwdrand().getBytes());
            token.setPassword(password.toCharArray());
        }
        AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPasswordHash(), getName());
        return  info;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
//        String name = (String)principals.getPrimaryPrincipal();
//        Set<String> set = new HashSet<String>();
//        set.add("admin");
//        User user = null;
//        try {
//            user = SecurityWebUtils.getUser(name);
//        } catch (QueryException e) {
//            e.printStackTrace();
//        }
//        if(user != null){
//            return new SimpleAuthorizationInfo(set);
//        }
        return null;
    }



}